{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 获取token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "ak = ''\n",
    "sk = ''\n",
    "\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={}&client_secret={}'.format(ak,sk)\n",
    "\n",
    "res = requests.post(host)\n",
    "print(res.text)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 图片base编码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import base64\n",
    "\n",
    "def get_img_base(file):\n",
    "    with open(file,'rb') as fp:\n",
    "        content = base64.b64encode(fp.read())\n",
    "        return content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "content = get_img_base('test1.jpg')\n",
    "content"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### api的调用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "token = '24.c8d6fe40022157bb39f71e0cf4845d27.2592000.1549328699.282335-15354396'\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/detect\"\n",
    "request_url = request_url + \"?access_token=\" + token\n",
    "\n",
    "params = {\n",
    "    'image':get_img_base('test2.jpg'),\n",
    "    'image_type':'BASE64',\n",
    "    'face_field':'age,beauty,gender'\n",
    "}\n",
    "\n",
    "res = requests.post(request_url,data=params)\n",
    "print(res.text)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 获取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pprint\n",
    "import json\n",
    "result = res.text\n",
    "json_result = json.loads(result)\n",
    "pprint.pprint(json_result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "code = json_result['error_code']\n",
    "gender = json_result['result']['face_list'][0]['gender']['type']\n",
    "beauty = json_result['result']['face_list'][0]['beauty']\n",
    "print(code,gender,beauty)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 颜值打分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import os\n",
    "path = 'row_img'\n",
    "list_paths = os.listdir(path)\n",
    "for list_path in list_paths:\n",
    "    img_path = path + '/' + list_path\n",
    "#     print(img_path)\n",
    "\n",
    "    token = '24.c8d6fe40022157bb39f71e0cf4845d27.2592000.1549328699.282335-15354396'\n",
    "\n",
    "    request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/detect\"\n",
    "    request_url = request_url + \"?access_token=\" + token\n",
    "\n",
    "    params = {\n",
    "        'image':get_img_base(img_path),\n",
    "        'image_type':'BASE64',\n",
    "        'face_field':'age,beauty,gender'\n",
    "    }\n",
    "\n",
    "    res = requests.post(request_url,data=params)\n",
    "    json_result = json.loads(res.text)\n",
    "    code = json_result['error_code']\n",
    "    if code == 222202:\n",
    "        continue\n",
    "        \n",
    "    try:\n",
    "        gender = json_result['result']['face_list'][0]['gender']['type']\n",
    "        if gender == 'male':\n",
    "            continue\n",
    "        beauty = json_result['result']['face_list'][0]['beauty']\n",
    "        print(list_path,beauty)\n",
    "            \n",
    "    except KeyError:\n",
    "        pass\n",
    "    except TypeError:\n",
    "        pass"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
